const express = require('express')
const apiRouter = express.Router()
const getWeChatUrlScheme = require('../wechat/getWeChatUrlScheme')
const saveFile = require('./api/saveEmail')
const { getNews, getTopicHome, getTopicDetail } = require('../database/mongoose')

apiRouter.get('/api', async (req, res) => {
    const { name } = req.query
    let query = {}
    let data = {}
    let result = {}
    switch (name) {
        case 'getWechatUrl':
            data = await getWeChatUrlScheme()
            break
        case 'getNews':
            query = {
                condition: req.query.condition || {},
                limit: parseInt(req.query.limit),
                skip: parseInt(req.query.skip)
            }
            result = await getNews(query)
            data = { newsList: result }
            break
        case 'getNewsDocument':
            query = {
                condition: { id: req.query.id },
                limit: 1,
                skip: 0
            }
            result = await getNews(query)
            data = { newsList: result }
            break
        case 'getTopic':
            if (req.query.subject === 'shimian' || req.query.subject === 'pifu' || req.query.subject === 'xuanyun') {
                result = await getTopicHome(req.query.subject)
                data = { topic: result }

            } else {
                data = { topic: '' }
            }
            break

        case 'getTopicDetail':
            query = {
                topic: req.query.topic,
                id: req.query.id
            }
            result = await getTopicDetail(query)
            data = { ...result }
            break
        case 'getDocumentHome':
            const subjects = [
                {
                    "title": "关于睡眠的话题",
                    "image": "/shimianImg.png",
                    "path": "/document/shimian",
                    "link": "失眠 >",
                    "content": "你是一沾枕头就能睡着的人吗？又或者你是一个总失眠的人，辗转反侧难以入眠。失眠的时候你一般会怎么办？"
                },
                {
                    "title": "皮肤病的防治",
                    "image": "/pifuImg.png",
                    "path": "/document/pifu",
                    "link": "皮肤病 >",
                    "content": "皮肤病是严重影响人民健康的常见病、多发病之一，皮肤病的发病率很高，多比较轻，但少数较重甚至可以危及生命。据这些常见的皮肤病提出了治疗的措施和注意事项。"
                },
                {
                    "title": "难以根治的疾病",
                    "image": "/xuanyunImg.png",
                    "path": "/document/xuanyun",
                    "link": "眩晕 >",
                    "content": "眩晕症虽然不像癌症那么致命，眩晕带给人的痛苦是持续影响了生活的品质，而且难以治愈，持续性长。为何眩晕逐渐成为医学上的难题呢？"
                }
            ]
            data = { subjects }
            break

        default:
            res.status(404)
            data = { errCode: 404, errMsg: `没有名字为${name}的这个api` }
    }

    res.json(data)
})

apiRouter.post('/api', async (req, res) => {
    postData = req.body
    console.log(postData)
    const { name } = req.query
    let data = {}
    switch (name) {
        case 'saveEmail':
            const res = await saveFile(postData)
            data = res
            break

        default:
            res.status(404)
            data = { errCode: 404, errMsg: `没有名字为${name}的这个api` }
    }

    res.json(data)

})

module.exports = apiRouter